﻿@{
    Layout = "Admin_Layout";
}
@section Style {
    <style>
    </style>
}
<div style="margin-left:20px;">
        <el-row  class="input-box">
            <el-col :span="24" >
            <div style="text-align:left;font-size:20px;font-weight:bold;">@ViewData["notice"]</div>
            <div style="background-color:black;height:1px; border:none;margin-top:10px;margin-bottom:15px;"></div>
            <div>
                <script id="editor" type="text/plain" style="width:1024px;height:300px;"></script>
            </div>
            </el-col>
        </el-row>
    <el-row class="input-box">
        <el-col :span="24">
            <div style="margin-top:20px;">
                <div style="text-align:left;font-size:20px;font-weight:bold;">视频和图片</div>
                <div style="background-color:black;height:1px; border:none;margin-top:10px;margin-bottom:15px;"></div>
                <div>
                    <div style="float:left;">
                        <div>
                            <video width="300" height="160" controls :src="videoUrl" style="background-color:silver;">
                                <source type="video/mp4">
                                <source type="video/avi">
                                <source type="video/webm">
                                您的浏览器不支持 video 属性。
                            </video>
                        </div>
                        <div>
                            <el-upload class="upload"
                                       accept=".mp4 , .webm"
                                       :show-file-list="false"
                                       action="string"
                                       :file-list="videoList"
                                       :auto-upload="false"
                                       :on-change="videoHandleChange"
                                       multiple="false">
                                <el-button slot="trigger" size="small" type="primary">展示视频</el-button>
                                <div slot="tip" class="el-upload__tip">[mp4/avi/webm视频，小于{{videoSize}}M]</div>
                            </el-upload>
                        </div>
                    </div>
                    <div style="float:left;margin-left:30px;">
                        <div>
                            <div style="float:left;"><img style="width:185px;height:160px;" :src="otherImgUrl1" title="请选择图片" /></div>
                            <div style="float:left;margin-left:10px;"><img style="width:185px;height:160px;" :src="otherImgUrl2" title="请选择图片" /></div>
                            <div style="float:left;margin-left:10px;"><img style="width:185px;height:160px;" :src="otherImgUrl3" title="请选择图片" /></div>
                        </div>
                        <div style="clear:both;"></div>
                        <div>
                            <el-upload class="upload"
                                       :limit="3"
                                       :on-exceed="otherHandleExceed"
                                       accept=".jpg , jpeg , .png"
                                       :show-file-list="false"
                                       list-type="picture"
                                       ref="upload"
                                       action="string"
                                       :auto-upload="false"
                                       :file-list="otherImgList"
                                       :on-change="otherHandleChange"
                                       multiple="true">
                                <el-button slot="trigger" size="small" type="primary" @@click="otherImgFile">展示图片</el-button>
                                <div slot="tip" class="el-upload__tip">[jpg/jpeg/png图片，每张小于{{imgSize}}K]</div>
                            </el-upload>
                        </div>
                    </div>
                </div>
            </div>
        </el-col>
    </el-row>
    <div style="background-color:black;height:1px; border:none;margin-top:20px;"></div>
    <div style="margin-bottom:15px;margin-top:15px;width:800px;display:none" id="progressDiv">
        <el-row style="margin-top:8px;display:flex;" :gutter="15">
            <el-col :span="6" style="text-align:right;margin-left:20px;font-size:15px;">
                提交进度
            </el-col>
            <el-col :span="12">
                <el-progress :text-inside="true" :stroke-width="20" :percentage="percentage"></el-progress>
            </el-col>
        </el-row>
    </div>
    <div style="margin-left:20px;width:800px;margin-top:20px;">
        <el-button type="primary" :disabled="submitBtnDisabled" @@click="submitForm" style="width:110px;">确 定</el-button>
        <el-button @@click="clearForm" style="width:110px;">清 空</el-button>
    </div>
</div>
@section Scripts {
    <script type="text/javascript" charset="gbk" src="/ueditor/ueditor.config.js?v3"></script>
    <script type="text/javascript" charset="gbk" src="/ueditor/ueditor.all.js?v3"></script>
    <script type="text/javascript" charset="gbk" src="/ueditor/ueditor.parse.js?v3"></script>
    <script type="text/javascript" charset="gbk" src="/ueditor/lang/zh-cn/zh-cn.js?v3"></script>
    <script type="text/javascript">
        var ue = UE.getEditor('editor');

        var app = new Vue({
            el: "#app",
            created() {
                this.$nextTick(() => {
                    ue.ready(function () {
                        ue.setHeight(300);//固定ueditor高度
                        app.loadData();
                    });
                })

                this.$nextTick().then(() => {
                })
            },
            mounted: async function() {
                this.editor = ue;
                $("#app").show();//vue渲染完后显示界面
            },
            methods: {
               async  loadData(){
                    await axios.post('../../Admin/AdminHome/GetPetFosterage', {
                        "datatype": this.datatype,
                    }).then(async res => {
                        if (res.data === null || res.data === undefined)
                                return;

                        await  this.restoreData(res.data);
                    }, error => {
                        alert("错误提示：" + error.message);
                    });
                },
               async  restoreData(data){
                   this.petfosterageid = data.petfosterageid;
                    this.editor.setContent(data.content,false);

                    this.videoUrl = '';
                    this.videoList = [];
                    if (data.videosrc !== '' && data.videosrc !== null) {
                        this.videoUrl = "/file/" + this.theDomain + "/videos/" + data.videosrc;
                    }

                    if (data.imgsrcs !== '' && data.imgsrcs !== null) {
                        app.otherImgList = [];

                        let imgUrls = data.imgsrcs.split('|');
                        for (var i = 0; i < imgUrls.length; i++) {
                            var imgurl = imgUrls[i];

                            var vueFileObj = await app.getVueFile("../../Admin/AdminHome/DownFile", imgurl);
                            app.otherImgList.push(vueFileObj);

                            if (i === 0)
                                app.otherImgUrl1 = URL.createObjectURL(vueFileObj.raw);
                            else if (i === 1)
                                app.otherImgUrl2 = URL.createObjectURL(vueFileObj.raw);
                            else if (i === 2)
                                app.otherImgUrl3 = URL.createObjectURL(vueFileObj.raw);
                        }
                    }

                },
                clearForm() {
                    this.editor.setContent('', false);

                    app.videoList = [];
                    app.otherImgList = [];
                    app.videoUrl = '';
                    app.otherImgUrl1 = '/img.jpg';
                    app.otherImgUrl2 = '/img.jpg';
                    app.otherImgUrl3 = '/img.jpg';
                },
                otherHandleExceed(files, fileList) {
                    this.$message.warning('只能选择三张图片');
                },
                otherImgFile() {
                    if (app.otherImgList.length < 3)
                        return;
                    app.otherImgList = [];
                    app.otherImgUrl1 = '/img.jpg';
                    app.otherImgUrl2 = '/img.jpg';
                    app.otherImgUrl3 = '/img.jpg';
                },
                otherHandleChange(file, fileList) {
                    let isLt10M = file.size / 1024 < this.imgSize;
                    if (!isLt10M) {
                        this.$message.error("图片大小不能超过" + this.imgSize + "KB!");
                        return;
                    }

                    // 限制类型只能为jpg,png
                    let isJPG = file.raw.type === 'image/jpeg';
                    let isPng = file.raw.type === 'image/png';
                    if (!isJPG && !isPng) {
                        this.$message.error("请上传正确格式的图片!");
                        return;
                    }

                    let URL = window.URL || window.webkitURL;
                    app.otherImgList = fileList;
                    for (var i = 0; i < app.otherImgList.length; i++) {
                        if (i === 0) {
                            app.otherImgUrl1 = URL.createObjectURL(app.otherImgList[i].raw);
                        }
                        else if (i === 1) {
                            app.otherImgUrl2 = URL.createObjectURL(app.otherImgList[i].raw);
                        }
                        else if (i === 2) {
                            app.otherImgUrl3 = URL.createObjectURL(app.otherImgList[i].raw);
                        }
                    }
                },
                videoHandleChange(file, fileList) {
                    let isLt10M = file.size / 1024 / 1024 < this.videoSize;
                    if (!isLt10M) {
                        this.$message.error("视频大小不能超过 " + this.videoSize + "MB!");
                        return;
                    }

                    // 限制类型
                    let isMp4 = file.raw.type === 'video/mp4';
                    let isAvi = file.raw.type === 'video/avi';
                    let isWebm = file.raw.type === 'video/webm';
                    if (!isMp4 && !isAvi && !isWebm) {
                        this.$message.error('请上传正确格式的视频');
                        return;
                    }

                    let URL = window.URL || window.webkitURL;

                    if (fileList.length > 0) {
                        app.videoList = [fileList[fileList.length - 1]]//这一步，是 展示最后一次选择文件
                        app.videoUrl = URL.createObjectURL(app.videoList[0].raw);
                    }
                },
                uploadProgress(progressEvent) {
                    /*
                     * progressEvent.loaded :已上传量
                     * progressEvent.total :上传的总大小
                    */
                    const p = Math.floor((progressEvent.loaded / progressEvent.total) * 100);
                    this.percentage = p;
                },
               async  submitForm(formName) {

                            //自定义验证
                   let content = this.editor.getContent();
                    if (content.length === 0) {
                        this.$message.warning('公告内容不能为空');
                        return;
                    }

                    if (content.length >10000) {
                                this.$message.warning('公告内容过长[文字样式和排版也占用内容长度计算]');
                                return;
                    }

                            let formData = new FormData();

                            //其他图片
                            for (var i = 0; i < this.otherImgList.length; i++) {
                                formData.append("otherImg" + i, this.otherImgList[i].raw);
                            }

                            //视频
                            for (var i = 0; i < this.videoList.length; i++) {
                                formData.append("video" + i, this.videoList[i].raw);
                            }

                            //文字内容
                            var textData = {
                        "petfosterageid": app.petfosterageid, "datatype": app.datatype, "content": content
                            };
                           formData.append("textData", JSON.stringify(textData));

                    let config = {
                        headers: {
                            'Content-Type': 'multipart/form-data;charset=utf-8',
                        },
                        transformRequest: [function (data) {
                            return data
                        }],
                        onUploadProgress: this.uploadProgress,
                    }

                    $("#progressDiv").show();
                    this.submitBtnDisabled = true;

                    await axios.post('../../Admin/AdminHome/AddOrUpdatePetFosterageInfo', formData, config).then(res => {
                                if (res.data=== "ok") {
                            this.percentage = 0;
                            $("#progressDiv").hide();
                            this.submitBtnDisabled = false;
                                    this.$message.success('提交成功');
                                    this.clearForm();
                                    this.loadData();
                                } else {
                            this.percentage = 0;
                            $("#progressDiv").hide();
                            this.submitBtnDisabled = false;
                                    this.$message.warning(res.data);
                                }
                            }, error => {
                        this.percentage = 0;
                        $("#progressDiv").hide();
                        this.submitBtnDisabled = false;
                                alert("错误提示：" + error.message);
                            });

                },
            },
            destroyed() {
                // 将editor进行销毁
                this.editor.destroy();
            },
            data() {
                return {
                    theDomain: '@Html.Raw(ViewData["theDomain"])',
                    submitBtnDisabled: false,
                    percentage: 0,
                    videoSize: @Html.Raw(ViewData["videoSize"]),
                    imgSize: @Html.Raw(ViewData["imgSize"]),
                    editor: null,
                    petfosterageid:-1,
                    datatype:@ViewData["datatype"],
                    defaultMenuActive: '@ViewData["defaultMenuActive"]',
                    otherImgList: [],
                    otherImgUrl1: '/img.jpg',
                    otherImgUrl2: '/img.jpg',
                    otherImgUrl3: '/img.jpg',
                    videoList: [],
                    videoUrl: '',
                }
            }
        });
    </script>
}
